package com.jshx.sz.gdjt.activity;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.jshx.sz.gdjt.R;
import com.jshx.sz.gdjt.constant.ApplicationGlobal;
import com.jshx.sz.gdjt.control.ToggleButtonView;
import com.jshx.sz.gdjt.control.ToggleButtonView.ToggleButtonStatu;
import com.jshx.sz.gdjt.db.DataBaseUtil;
/**
 * 传动Fragment
 * @author xiangchangan
 * @time 2013-08-23
 */
public class DriveFrame extends Fragment 
{
	//正常不正常开关组件1
	private ToggleButtonView toggleBtn1;
	
	//正常不正常开关组件2
	private ToggleButtonView toggleBtn2;
	
	//正常不正常开关组件3
	private ToggleButtonView toggleBtn3;
	
	//正常不正常开关组件4
	private ToggleButtonView toggleBtn4;
	
	//正常不正常开关组件5
	private ToggleButtonView toggleBtn5;
	
	//图片显示容器1
	private LinearLayout imagesLayout1;
	
	//图片显示容器2
	private LinearLayout imagesLayout2;
	
	//图片显示容器3
	private LinearLayout imagesLayout3;
	
	//图片显示容器4
	private LinearLayout imagesLayout4;
	
	//图片显示容器5
	private LinearLayout imagesLayout5;
	
	//图片路径存放容器1
	private ArrayList<String> images1;
	
	//图片路径存放容器2
	private ArrayList<String> images2;
	
	//图片路径存放容器3
	private ArrayList<String> images3;
	
	//图片路径存放容器4
	private ArrayList<String> images4;
	
	//图片路径存放容器5
	private ArrayList<String> images5;
	
	/**
	 * 组件实例化工具
	 */
	private LayoutInflater layoutInflater;
	
	/**
	 * 图片地址路径
	 */
	private String picName;
	
	//紧急按钮
	private Button driveCall;
	
	//页面数据
	private HashMap<String, String> data;
	
	//
	private static String YES = "1";
	
	//
	private static String NO = "0";
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.drive_fragment, null);
		layoutInflater = LayoutInflater.from(this.getActivity());
		findView(view);
		initView();
		return view;
	}

	/**
	 * 组件实例化
	 * @param view
	 */
	private void findView(View view) {
		toggleBtn1 = (ToggleButtonView)view.findViewById(R.id.drive_toggle_btn1);
		toggleBtn2 = (ToggleButtonView)view.findViewById(R.id.drive_toggle_btn2);
		toggleBtn3 = (ToggleButtonView)view.findViewById(R.id.drive_toggle_btn3);
		toggleBtn4 = (ToggleButtonView)view.findViewById(R.id.drive_toggle_btn4);
		toggleBtn5 = (ToggleButtonView)view.findViewById(R.id.drive_toggle_btn5);
		imagesLayout1 = (LinearLayout)view.findViewById(R.id.drive_imagelayout_1);
		imagesLayout2 = (LinearLayout)view.findViewById(R.id.drive_imagelayout_2);
		imagesLayout3 = (LinearLayout)view.findViewById(R.id.drive_imagelayout_3);
		imagesLayout4 = (LinearLayout)view.findViewById(R.id.drive_imagelayout_4);
		imagesLayout5 = (LinearLayout)view.findViewById(R.id.drive_imagelayout_5);
		driveCall=(Button)view.findViewById(R.id.drive_call);
	}

	/**
	 * 组件数据初始化
	 */
	private void initView() {
		
		driveCall.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				Intent  intent=new Intent();
				//激活源代码,添加intent对象
				intent.setAction("android.intent.action.CALL");
				//intent.addCategory("android.intent.category.DEFAULT");内部会自动添加类别，
				intent.setData(Uri.parse("tel:15306205760"));
				//激活Intent
				startActivity(intent);
			}
		});
		toggleBtn1.setLeftButtonClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) {
				if(images1 != null)
				{
					images1.clear();
				}
				imagesLayout1.removeAllViews();
				imagesLayout1.setVisibility(View.GONE);
			}
		});
		toggleBtn1.setRightButtonClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) {
				if(!imagesLayout1.isShown())
				{
					imagesLayout1.setVisibility(View.VISIBLE);
					images1 = new ArrayList<String>();
					images1.add(new String());
					refreshImages(1, images1);
					goCameraTakePhoto(1);
				}
			}
		});
		
		toggleBtn2.setLeftButtonClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) {
				if(images2 != null)
				{
					images2.clear();
				}
				imagesLayout2.removeAllViews();
				imagesLayout2.setVisibility(View.GONE);
			}
		});
		toggleBtn2.setRightButtonClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) {
				if(!imagesLayout2.isShown())
				{
					imagesLayout2.setVisibility(View.VISIBLE);
					images2 = new ArrayList<String>();
					images2.add(new String());
					refreshImages(2, images2);
					goCameraTakePhoto(2);
				}
			}
		});
		
		toggleBtn3.setLeftButtonClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) {
				if(images3 != null)
				{
					images3.clear();
				}
				imagesLayout3.removeAllViews();
				imagesLayout3.setVisibility(View.GONE);
			}
		});
		toggleBtn3.setRightButtonClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) {
				if(!imagesLayout3.isShown())
				{
					imagesLayout3.setVisibility(View.VISIBLE);
					images3 = new ArrayList<String>();
					images3.add(new String());
					refreshImages(3, images3);
					goCameraTakePhoto(3);
				}
			}
		});
		
		toggleBtn4.setLeftButtonClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) {
				if(images4 != null)
				{
					images4.clear();
				}
				imagesLayout4.removeAllViews();
				imagesLayout4.setVisibility(View.GONE);
			}
		});
		toggleBtn4.setRightButtonClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) {
				if(!imagesLayout4.isShown())
				{
					imagesLayout4.setVisibility(View.VISIBLE);
					images4 = new ArrayList<String>();
					images4.add(new String());
					refreshImages(4, images4);
					goCameraTakePhoto(4);
				}
			}
		});
		
		toggleBtn5.setLeftButtonClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) {
				if(images5 != null)
				{
					images5.clear();
				}
				imagesLayout5.removeAllViews();
				imagesLayout5.setVisibility(View.GONE);
			}
		});
		toggleBtn5.setRightButtonClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) {
				if(!imagesLayout5.isShown())
				{
					imagesLayout5.setVisibility(View.VISIBLE);
					images5 = new ArrayList<String>();
					images5.add(new String());
					refreshImages(5, images5);
					goCameraTakePhoto(5);
				}
			}
		});
		data = ((TaskActivityGroup)getActivity()).getDataFragment3();
	}
	
	@Override
	public void onResume() {
		initViewData();
		super.onResume();
	}
	
	/**
	 * 
	 * @param list
	 * @return
	 */
	private String arrayToString(ArrayList<String> list)
	{
		if(list == null || list.size() < 1)
		{
			return "";
		}
		else
		{
			String temp = "";
			for (int i = 0; i < list.size(); i++) {
				if(i == list.size() - 1)
				{
					temp += list.get(i);
				}
				else
				{
					temp += list.get(i) + ",";
				}
			}
			return temp;
		}
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) 
	{
		if(resultCode == Activity.RESULT_OK)
		{
			String path = ApplicationGlobal.imageBaseFold + picName;
			if(requestCode == 1)
			{
				images1.add(0, path);
				refreshImages(1, images1);
			}
			else if(requestCode == 2)
			{
				images2.add(0, path);
				refreshImages(2, images2);
			}
			else if(requestCode == 3)
			{
				images3.add(0, path);
				refreshImages(3, images3);
			}
			else if(requestCode == 4)
			{
				images4.add(0, path);
				refreshImages(4, images4);
			}
			else if(requestCode == 5)
			{
				images5.add(0, path);
				refreshImages(5, images5);
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	/**
	 * 刷新图片数据并更新视图
	 */
	private void refreshImages(final int type, final ArrayList<String> data) 
	{
		if(data != null)
		{
			if(type == 1)
			{
				imagesLayout1.removeAllViews();
			}
			else if(type == 2)
			{
				imagesLayout2.removeAllViews();
			}
			else if(type == 3)
			{
				imagesLayout3.removeAllViews();
			}
			else if(type == 4)
			{
				imagesLayout4.removeAllViews();
			}
			else if(type == 5)
			{
				imagesLayout5.removeAllViews();
			}
			for (int i = 0; i < data.size(); i++) 
			{
				if(i == data.size() - 1)
				{
					ImageView view = new ImageView(DriveFrame.this.getActivity());
					view.setBackgroundDrawable(getResources().getDrawable(R.drawable.addimage_select));
					LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
					lp.gravity = Gravity.CENTER_VERTICAL;
					view.setLayoutParams(lp);
					view.setOnClickListener(new OnClickListener() 
					{
						@Override
						public void onClick(View v) {
							goCameraTakePhoto(type);
						}
					});
					if(type == 1)
					{
						imagesLayout1.addView(view);
					}
					else if(type == 2)
					{
						imagesLayout2.addView(view);
					}
					else if(type == 3)
					{
						imagesLayout3.addView(view);
					}
					else if(type == 4)
					{
						imagesLayout4.addView(view);
					}
					else if(type == 5)
					{
						imagesLayout5.addView(view);
					}
				}
				else
				{
					View view = layoutInflater.inflate(R.layout.image_gallry_item, null);
					ImageView image = (ImageView)view.findViewById(R.id.gallery_image);
					ImageButton del = (ImageButton)view.findViewById(R.id.image_delete);
					Options option = new Options();
					option.inSampleSize = 2;
					final int index = i;
					final Drawable d = new BitmapDrawable(BitmapFactory.decodeFile(data.get(i), option));
					image.setImageDrawable(d);
					del.setOnClickListener(new OnClickListener() 
					{
						@Override
						public void onClick(View v) {
							if(type == 1)
							{
								images1.remove(index);
								refreshImages(type, images1);
							}
							else if(type == 2)
							{
								images2.remove(index);
								refreshImages(type, images2);
							}
							else if(type == 3)
							{
								images3.remove(index);
								refreshImages(type, images3);
							}
							else if(type == 4)
							{
								images4.remove(index);
								refreshImages(type, images4);
							}
							else if(type == 5)
							{
								images5.remove(index);
								refreshImages(type, images5);
							}
						}
					});
					view.setOnClickListener(new OnClickListener() 
					{
						@Override
						public void onClick(View v) {
							Intent intent = new Intent(DriveFrame.this.getActivity(), PopActivity.class);
							intent.putExtra("isImage", true);
							ApplicationGlobal.globalDrawable = d;
							startActivity(intent);
						}
					});
					if(type == 1)
					{
						imagesLayout1.addView(view);
					}
					else if(type == 2)
					{
						imagesLayout2.addView(view);
					}
					else if(type == 3)
					{
						imagesLayout3.addView(view);
					}
					else if(type == 4)
					{
						imagesLayout4.addView(view);
					}
					else if(type == 5)
					{
						imagesLayout5.addView(view);
					}
				}
			}
		}
	}
	
	/**
	 * 跳转拍照界面
	 */
	private void goCameraTakePhoto(int type)
	{
		Intent intent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		picName = format.format(new Date()) + ".jpg";
		File f = new File(ApplicationGlobal.imageBaseFold + picName);
		if(!f.exists())
		{
			try {
				f.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		Uri u = Uri.fromFile(f);
		intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, u);
		startActivityForResult(intent, type);//跳转至系统自带拍照界面
	}

	/**
	 * 获取页面数据
	 * @return
	 */
	public HashMap<String, String> getData() 
	{
		if(data == null)
		{
			data = new HashMap<String, String>();
		}
		data.put(DataBaseUtil.task_detail_type2_table_clos[26], toggleBtn1.getToggleButtonStatu() 
				== ToggleButtonView.ToggleButtonStatu.STATU_LEFT ? YES : NO);
		data.put(DataBaseUtil.task_detail_type2_table_clos[27], arrayToString(images1));
		data.put(DataBaseUtil.task_detail_type2_table_clos[28], toggleBtn2.getToggleButtonStatu() 
				== ToggleButtonView.ToggleButtonStatu.STATU_LEFT ? YES : NO);
		data.put(DataBaseUtil.task_detail_type2_table_clos[29], arrayToString(images2));
		data.put(DataBaseUtil.task_detail_type2_table_clos[30], toggleBtn3.getToggleButtonStatu() 
				== ToggleButtonView.ToggleButtonStatu.STATU_LEFT ? YES : NO);
		data.put(DataBaseUtil.task_detail_type2_table_clos[31], arrayToString(images3));
		data.put(DataBaseUtil.task_detail_type2_table_clos[32], toggleBtn4.getToggleButtonStatu() 
				== ToggleButtonView.ToggleButtonStatu.STATU_LEFT ? YES : NO);
		data.put(DataBaseUtil.task_detail_type2_table_clos[33], arrayToString(images4));
		data.put(DataBaseUtil.task_detail_type2_table_clos[34], toggleBtn5.getToggleButtonStatu() 
				== ToggleButtonView.ToggleButtonStatu.STATU_LEFT ? YES : NO);
		data.put(DataBaseUtil.task_detail_type2_table_clos[35], arrayToString(images5));
		return data;
	}

	/**
	 * 设置页面数据
	 * @param data
	 */
	public void initViewData() 
	{
		if(data!=null&&data.size()<1)
		{
			
		}
		
		if(data != null)
		{
			if(data.get(DataBaseUtil.task_detail_type2_table_clos[26]) != null
					&& data.get(DataBaseUtil.task_detail_type2_table_clos[26]).equals(NO))
			{
				toggleBtn1.setToggleButtonStatu(ToggleButtonStatu.STATU_RIGHT);
				imagesLayout1.setVisibility(View.VISIBLE);
				images1 = new ArrayList<String>();
				images1.add(new String());
				if(data.get(DataBaseUtil.task_detail_type2_table_clos[27]) != null)
				{
					String[] temp = data.get(DataBaseUtil.task_detail_type2_table_clos[27]).split(",");
					if(temp != null && temp.length > 0)
					{
						for (int i = 0; i < temp.length; i++) {
							if(temp[i] != null && temp[i].length() > 0)
							{
								images1.add(0, temp[i]);
							}
						}
					}
				}
				refreshImages(1, images1);
			}
			if(data.get(DataBaseUtil.task_detail_type2_table_clos[28])!=null
					&& data.get(DataBaseUtil.task_detail_type2_table_clos[28]).equals(NO))
			{
				toggleBtn2.setToggleButtonStatu(ToggleButtonStatu.STATU_RIGHT);
				imagesLayout2.setVisibility(View.VISIBLE);
				images2 = new ArrayList<String>();
				images2.add(new String());
				if(data.get(DataBaseUtil.task_detail_type2_table_clos[29]) != null)
				{
					String[] temp = data.get(DataBaseUtil.task_detail_type2_table_clos[29]).split(",");
					if(temp != null && temp.length > 0)
					{
						for (int i = 0; i < temp.length; i++) {
							if(temp[i] != null && temp[i].length() > 0)
							{
								images2.add(0, temp[i]);
							}
						}
					}
				}
				refreshImages(2, images2);
			}
			if(data.get(DataBaseUtil.task_detail_type2_table_clos[30])!=null
					&& data.get(DataBaseUtil.task_detail_type2_table_clos[30]).equals(NO))
			{
				toggleBtn3.setToggleButtonStatu(ToggleButtonStatu.STATU_RIGHT);
				imagesLayout3.setVisibility(View.VISIBLE);
				images3 = new ArrayList<String>();
				images3.add(new String());
				if(data.get(DataBaseUtil.task_detail_type2_table_clos[31]) != null)
				{
					String[] temp = data.get(DataBaseUtil.task_detail_type2_table_clos[31]).split(",");
					if(temp != null && temp.length > 0)
					{
						for (int i = 0; i < temp.length; i++) {
							if(temp[i] != null && temp[i].length() > 0)
							{
								images3.add(0, temp[i]);
							}
						}
					}
				}
				refreshImages(3, images3);
			}
			if(data.get(DataBaseUtil.task_detail_type2_table_clos[32])!=null
					&& data.get(DataBaseUtil.task_detail_type2_table_clos[32]).equals(NO))
			{
				toggleBtn4.setToggleButtonStatu(ToggleButtonStatu.STATU_RIGHT);
				imagesLayout4.setVisibility(View.VISIBLE);
				images4 = new ArrayList<String>();
				images4.add(new String());
				if(data.get(DataBaseUtil.task_detail_type2_table_clos[33]) != null)
				{
					String[] temp = data.get(DataBaseUtil.task_detail_type2_table_clos[33]).split(",");
					if(temp != null && temp.length > 0)
					{
						for (int i = 0; i < temp.length; i++) {
							if(temp[i] != null && temp[i].length() > 0)
							{
								images4.add(0, temp[i]);
							}
						}
					}
				}
				refreshImages(4, images4);
			}
			if(data.get(DataBaseUtil.task_detail_type2_table_clos[34])!=null
					&& data.get(DataBaseUtil.task_detail_type2_table_clos[34]).equals(NO))
			{
				toggleBtn5.setToggleButtonStatu(ToggleButtonStatu.STATU_RIGHT);
				imagesLayout5.setVisibility(View.VISIBLE);
				images5 = new ArrayList<String>();
				images5.add(new String());
				if(data.get(DataBaseUtil.task_detail_type2_table_clos[35]) != null)
				{
					String[] temp = data.get(DataBaseUtil.task_detail_type2_table_clos[35]).split(",");
					if(temp != null && temp.length > 0)
					{
						for (int i = 0; i < temp.length; i++) {
							if(temp[i] != null && temp[i].length() > 0)
							{
								images5.add(0, temp[i]);
							}
						}
					}
				}
				refreshImages(5, images5);
			}
		}
	}
}
